package com.white.mysoundrecorder;

import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

import java.io.IOException;

import static android.R.attr.path;

/**
 * Created by ytf on 2017/8/30 030.
 * Description:
 */

public class Player
{
    private MediaPlayer player;

    private static Player instance;
    private RecordItem currentItem = null;
    private int currentPos;

    private Player()
    {
        currentPos = -1;
        player = new MediaPlayer();
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
        {
            @Override
            public void onCompletion(MediaPlayer mp)
            {
                if(l != null)
                {
                    mHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            l.onComplete();
                        }
                    });
                }
            }
        });
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
        {
            @Override
            public void onPrepared(MediaPlayer mp)
            {
                player.start();
                currentItem.playState = st_playing;
                new ListenThread().start();
                if(l != null)
                {
                    mHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            l.onStart(player.getDuration());
                        }
                    });
                }
            }
        });
    }

    public static Player getInstance()
    {
        if(instance == null)
        {
            synchronized (Player.class)
            {
                if(instance == null)
                {
                    instance = new Player();
                }
            }
        }
        return instance;
    }

    public static final int st_default = 0;
    public static final int st_preparing = 1;
    public static final int st_playing = 2;
    public static final int st_pause = 3;

    public void start(RecordItem item, int position)
    {
        stop(currentItem, currentPos);
        currentItem = item;
        currentPos = position;
        if(player != null && !player.isPlaying())
        {
            try
            {
                player.setDataSource(item.filePath);
                player.prepareAsync();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    private Handler mHandler = new Handler(Looper.myLooper())
    {
        @Override
        public void handleMessage(Message msg)
        {
            int p = msg.arg1;
            if(currentPos != -1 && currentItem != null)
            {
                currentItem.playProgress = p;
                if(l != null)
                {
                    l.onProgress(p, currentPos);
                }
            }
        }
    };

    class ListenThread extends Thread
    {
        @Override
        public void run()
        {
            while(player.isPlaying())
            {
                int p = player.getCurrentPosition();
                Message msg = mHandler.obtainMessage();
                msg.arg1 = p;
                mHandler.sendMessage(msg);
                SystemClock.sleep(10);
            }
        }
    }

    public void pause()
    {
        if(player != null && !player.isPlaying())
        {
            player.pause();
            currentItem.playState = st_pause;
            if(l != null)
            {
                l.onPause();
            }
        }
    }

    public void resume()
    {
        if(player != null && !player.isPlaying())
        {
            player.start();
            currentItem.playState = st_playing;
            if(l != null)
            {
                l.onResume();
            }
            new ListenThread().start();
        }
    }

    public void stop(RecordItem currentItem, int pos)
    {
        if(player != null)
        {
            player.stop();
            if(currentItem != null)
            {
                currentItem.playState = st_default;
                if(l != null)
                {
                    l.onStop(currentItem, pos);
                }
            }
        }
    }

    public void destroy()
    {
        if(player != null)
        {
            player.stop();
            player.release();
        }
    }

    public void setPlayListener(PlayListener v)
    {
        l = v;
    }

    PlayListener l;

    public interface PlayListener
    {
        void onComplete();

        void onStart(int s);

        void onPause();

        void onStop(RecordItem item, int pos);

        void onResume();

        void onProgress(int p, int position);
    }
}
